package com.itboye.boyeshoping.fragment

import android.content.Intent
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.itboye.boyeshoping.R
import com.itboye.boyeshoping.ViewUtil.XGridView
import com.itboye.boyeshoping.activity.GoodsDetailActivity
import com.itboye.boyeshoping.activity.SearchGoodsActivity
import com.itboye.boyeshoping.adapter.HomeGoodsAdapter
import com.itboye.boyeshoping.base.BaseFragment
import com.itboye.boyeshoping.constants.NetPublicConstant
import com.itboye.boyeshoping.util.BasePtr
import com.jude.rollviewpager.OnItemClickListener
import com.jude.rollviewpager.RollPagerView
import com.jude.rollviewpager.adapter.LoopPagerAdapter
import com.jude.rollviewpager.hintview.IconHintView

/**
 * Created by Administrator on 2018/6/27 0027.
 */
class HomeFragment : BaseFragment() ,View.OnClickListener{


    //title
    private lateinit var backRl: RelativeLayout
    private lateinit var title:TextView
    private lateinit var rightImg:ImageView

    private lateinit var goodsGv:XGridView
    private lateinit var lunbo:RollPagerView
    private lateinit var goodsAdapter:HomeGoodsAdapter
    override fun initView(): Int {
        return R.layout.fragment_home
    }

    override fun onMyActivityCreated() {

         backRl= activity!!.findViewById(R.id.backRl) as RelativeLayout
         title= activity!!.findViewById(R.id.title) as TextView
         rightImg= activity!!.findViewById(R.id.rightImg) as ImageView
         goodsGv= activity!!.findViewById(R.id.goodsGv) as XGridView
         lunbo= activity!!.findViewById(R.id.lunbo) as RollPagerView

         rightImg.setBackgroundDrawable(resources.getDrawable(R.mipmap.sousuo_white))
         title.text = "博也商城"
         goodsAdapter=HomeGoodsAdapter(activity)
        goodsGv.adapter=goodsAdapter
        lunbo.isFocusable = true
        lunbo.isFocusableInTouchMode = true
        lunbo.requestFocus()

        initLunbo()
        goodsGv.onItemClickListener = AdapterView.OnItemClickListener { _, _, _, _ ->
            activity!!.startActivity(Intent(activity,GoodsDetailActivity::class.java))
        }
    }

    override fun onClick(p0: View?) {
       when(p0!!.id){
           R.id.rightImg->{//搜索

               activity!!.startActivity(Intent(activity, SearchGoodsActivity::class.java))
           }


       }
    }
    private fun initLunbo() {
//
//        lunbo.setPlayDelay(3000)
//        val testLoopAdapter = TestLoopAdapter(lunbo)
//        testLoopAdapter.setImgs(LunboBean)
//        lunbo.setAdapter(testLoopAdapter)
//        lunbo.setHintView(IconHintView(activity, R.drawable.lunbo_check, R.drawable.lunbo_uncheck, 28))
    }
//
//
//    //轮播adapter
//    private inner class TestLoopAdapter(viewPager: RollPagerView) : LoopPagerAdapter(viewPager) {
//        internal lateinit var urlList: List<String>
//
//        fun setImgs(Lunbo: List<String>) {
//            this.urlList = Lunbo
//            notifyDataSetChanged()
//        }
//
//        override fun getView(container: ViewGroup, position: Int): View {
//            val view = ImageView(container.context)
//            view.setOnClickListener { Log.i("RollViewPager", "onClick") }
//            view.scaleType = ImageView.ScaleType.CENTER_CROP
//            view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
//            Glide.with(activity)
//                    .load(NetPublicConstant.IMAGE_URL + urlList[position].getImg())
//                    .into(view)
//            view.setOnClickListener {
//                if ("" != urlList[position].getUrl()) {
//                    val intent = Intent(activity, LunBoWebActivity::class.java)
//                    intent.putExtra("url", urlList[position].getUrl())
//                    startActivity(intent)
//                }
//            }
//            return view
//        }
//
//        override fun getRealCount(): Int {
//            return urlList.size
//        }

//    }

}